import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:google_fonts/google_fonts.dart';

import '../Gloabal.dart';


// 标题Widget
class BeautifulTitleWidget extends StatelessWidget{
  final data;

  BeautifulTitleWidget(this.data);

  @override
  Widget build(BuildContext context) {
    bool isDarkTheme = Theme.of(context).brightness == Brightness.dark;
    print("Value=$isDarkTheme");

    TextStyle styleWithTheme = isDarkTheme
      ?
      TextStyle(
        fontFamily: "JetBrainsMono",
        fontSize: 25,
        fontWeight: FontWeight.w500,
        color: Colors.white70,
        letterSpacing: 1,
        height: 1.2,
      )
      :
      TextStyle(
        fontFamily: "JetBrainsMono",
        fontSize: 25,
        fontWeight: FontWeight.w500,
        color: Colors.blueGrey.shade800,
        letterSpacing: 1,
        height: 1.2,
      );

    return Text(
      data,
      style: styleWithTheme
    );
  }
}

// 抽屉Widget
class MenuItemInfo{
  final icon;
  final title;

  MenuItemInfo(this.icon,this.title);
}

class _MenuItem extends StatelessWidget {
  final MenuItemInfo item;
  final int index;
  final updateTabBarView;
  _MenuItem(this.item, this.index, this.updateTabBarView);

  @override
  Widget build(BuildContext context) {

    return ListTile(
      leading: Icon(item.icon),
      title: Text(item.title),
      onTap: () {
        TabController controller = DefaultTabController.of(context);
        int currentTab = controller.index;
        print("change=${currentTab}");

        var target_id = GlobalSettings.Tab2ContentMapping[currentTab]!.toInt() + index;
        GlobalSettings.AppTabContent[currentTab] = GlobalSettings.Content[target_id];
        this.updateTabBarView(currentTab,GlobalSettings.AppTabContent);

        Navigator.pop(context);
      },
    );
  }
}

class MenuWidget extends StatelessWidget {
  final items;
  final updateTabBarView;

  MenuWidget(this.items,this.updateTabBarView);

  @override
  Widget build(BuildContext context) {
    final DRAWERHEAD = DrawerHeader(
      decoration: BoxDecoration(color: Colors.teal),
      child: Align(
        alignment: Alignment.centerLeft,
        child:Column(
          children: [
            Text('课程信息', style: TextStyle(fontFamily: "JetBrainsMono", color: Colors.white, fontSize: 24)),
            Text('Syllabus', style: TextStyle(fontFamily: "JetBrainsMono" ,color: Colors.white, fontSize: 18)),
          ],
        ),
      ),
    );

    TabController controller = DefaultTabController.of(context);
    int currentTab = controller.index;

    return Drawer(
      child: ListView.builder(
          padding: EdgeInsets.zero,
          itemCount: this.items.length + 1, // +1 用于 header
          itemBuilder: (context, index) {
            if (index == 0) return DRAWERHEAD;
            else return _MenuItem(this.items[currentTab][index - 1],index - 1,updateTabBarView);
          }
      ),
    );
  }
}


// 搜索图标Widget（TODO：后面可以做成头像框）
class AvatarWidget extends StatelessWidget{

  @override
  Widget build(BuildContext context) {
    return IconButton(
      onPressed: () {
        Navigator.pushNamed(context,"/search");
      },
      icon: Icon(
        Icons.search_sharp,
        // color: Colors.black45,
        size: 30.0,
      ),
    );
  }
}

// 浮动按钮Widget
class FloatingButtonWidget extends StatelessWidget{
  var fabVisible;
  var setTheme;

  FloatingButtonWidget(this.fabVisible,this.setTheme);

  @override
  Widget build(BuildContext context) {
    return FloatingActionButton(
      backgroundColor: Colors.transparent,
      elevation: 0,
      shape: CircleBorder(),
      onPressed: () {
        setTheme();
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(
            behavior: SnackBarBehavior.floating,
            margin: const EdgeInsets.only(
              bottom: 50,
              left: 50,
              right: 50
            ),
            shape: RoundedRectangleBorder(borderRadius:BorderRadius.all(Radius.circular(12))),
            duration: Durations.long1,
            content: Column(
              children: [
                Text('改变了主题'),
                Text('The theme has changed.'),
              ],
            )
          ),
        );
      },
      child: const Icon(Icons.format_paint_outlined),
    );
  }
}